package com.cloudlinkscm.base.authcenter.domain;

import com.cloudlinkscm.base.authcenter.assembler.PublicResourceCreateDto2PublicResourceAssembler;
import com.cloudlinkscm.base.authcenter.constant.ResourceTypeEnum;
import com.cloudlinkscm.base.authcenter.dao.PublicResourceDao;
import com.cloudlinkscm.base.authcenter.model.PublicResource;
import com.cloudlinkscm.base.authcenter.model.dto.*;
import com.cloudlinkscm.base.authcenter.model.dto.ResourceTree;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * @author : tac
 * @date : 28/07/2017
 */
@Component
public class AbstractPublicResourceManager {

	@Autowired
    private PublicResourceDao dao;

    @Autowired
    private AbstractTenantResourceManager tenantResourceManager;

    @Autowired
    private ResourceManager resourceManager;

    @Autowired
    private PublicResourceCreateDto2PublicResourceAssembler publicResourceCreateDto2PublicResourceAssembler;

    public PublicResource createModule(PublicResourceCreateDto dto){
        return doCreate(dto, ResourceTypeEnum.MODULE);
    }
    public PublicResource createMenu(SubPublicResourceCreateDto dto){
        return doCreate(dto, ResourceTypeEnum.MENU);
    }
    public PublicResource createPage(PermissionPublicResourceCreateDto dto){
        return doCreate(dto, ResourceTypeEnum.PAGE);
    }
    public PublicResource createAction(PermissionPublicResourceCreateDto dto){
        return doCreate(dto, ResourceTypeEnum.ACTION);
    }

    /**
     * 删除公共资源
     *
     * <ul>
     *     <li>将递归删除所有子资源</li>
     *     <li>将删除从该资源fork的租户资源</li>
     * </ul>
     */
    public int delete(String id) {
        int rowNum = 0;
        rowNum += dao.deleteByPrimaryKeyRecursively(id);
        rowNum += tenantResourceManager.deleteByPubId(id);
        return rowNum;
    }

	public PageInfo<PublicResource> list(PublicResourceQueryDto dto){
        PageHelper.startPage(dto.getPageIndex(), dto.getPageSize());
        //todo:: 实现查询
        List<PublicResource> resources = dao.selectAll();
        PageInfo<PublicResource> pageInfo = new PageInfo<>(resources);
        return pageInfo;
    }

	/**
	 * 公共资源：所有的 resource
	 *
	 * @return
	 */
	public List<PublicResource> listAll(){
        return dao.selectAll();
    }

	/**
	 * 公共资源：所有的 resource
	 *
	 * @return
	 */
	public List<ResourceTree> tree() {
		return tree(listAll());
	}


	/**
	 * 公共资源 tree
	 *
	 * @param publicResourceList
	 * @return
	 */
	public List<ResourceTree> tree(List<PublicResource> publicResourceList) {
		return resourceManager.buildResourceTree(
				resourceManager.convertResource(publicResourceList));
	}

	/**
	 * 公共资源：创建
	 *
	 * @param dto
	 * @param type
	 * @return
	 */
    protected PublicResource doCreate(PublicResourceCreateDto dto, ResourceTypeEnum type) {
        int rowNum = 0;
        PublicResource resource  = publicResourceCreateDto2PublicResourceAssembler.assemble(dto, type);
        rowNum += dao.insert(resource);
        return resource;
    }

	/**
	 * 公共资源：更新
	 *
	 * <strong>
	 *     需要更新 shiro 资源权限
	 * </strong>
	 *
	 * @param updateDTO
	 * @return
	 */
	public PublicResource update(PublicResourceUpdateDTO updateDTO) {

		// TODO: 需要去更新 shiro 的权限
		PublicResource publicResource = new PublicResource();
		BeanUtils.copyProperties(updateDTO, publicResource);

		dao.updateByPrimaryKeySelective(publicResource);
		return publicResource;
	}
}
